##############################################
### read data #####

library('data.table')


d <- fread('annual_data_markups.csv',drop = 'V1')
d$fyear <- as.numeric(d$fyear)

d$date <- d$fyear

d <- d[cogs > 0 & sale > 0 & emp > 0]

library('lfe')

d$industry <- d$FF49

N <- max(d$period)
source('../lag.R')

dl <- d[,.(period=1:N,
           sale.l1 = lag(sale, period, 1, N),
           emp.l1   = lag(emp, period, 1, N),
           cogs.l1  = lag(cogs, period, 1, N),
           xlr.l1   = lag(xlr, period, 1, N)), by = c('gvkey')]

d <- merge(d, dl, by = c('gvkey', 'period'))

d <- d[complete.cases(d[,c('sale','sale.l1','emp','emp.l1')])]

################################################
########
################################################

d$sale.g1 <- (d$sale - d$sale.l1)/(.5*d$sale + .5*d$sale.l1)
d$emp.g1 <- (d$emp - d$emp.l1)/(.5*d$emp + .5*d$emp.l1)
d$cogs.g1 <- (d$cogs - d$cogs.l1)/(.5*d$cogs + .5*d$cogs.l1)
d$xlr.g1 <- (d$xlr - d$xlr.l1)/(.5*d$xlr + .5*d$xlr.l1)

d$emp  <- log(d$emp)
d$sale <- log(d$sale)
d$cogs <- log(d$cogs)
d$xlr  <- log(d$xlr)

#######################
### Wage bill #####

d1 <- d[date > 1980 & date < 2019]
d1 <- d1[complete.cases(d1[,'xlr'])]
d1 <- d1[xlr > -Inf]

summary(felm(xlr ~ sale | factor(industry):factor(date), data = d1))

summary(felm(xlr ~ sale | factor(industry):factor(date) + factor(gvkey), data = d1))

summary(felm(xlr.g1 ~ sale.g1 | factor(industry):factor(date), data = d1))

#######################
### Labor force #####

d1 <- d[date > 1980 & date < 2019]
d1 <- d1[complete.cases(d1[,'emp'])]

summary(felm(emp ~ sale | factor(industry):factor(date), data = d1))

summary(felm(emp ~ sale | factor(industry):factor(date) + factor(gvkey), data = d1))

summary(felm(emp.g1 ~ sale.g1 | factor(industry):factor(date), data = d1))

#######################
### COGS #####

d1 <- d[date > 1980 & date < 2019]
d1 <- d1[complete.cases(d1[,'cogs'])]

summary(felm(cogs ~ sale | factor(industry):factor(date), data = d1))

summary(felm(cogs ~ sale | factor(industry):factor(date) + factor(gvkey), data = d1))

summary(felm(cogs.g1 ~ sale.g1 | factor(industry):factor(date), data = d1))

d1$mkp.g1 <- d1$sale.g1 - d1$cogs.g1

x <- felm(sale.g1 ~ 0 | factor(industry):factor(date), data = d1)$residuals
y <- felm(mkp.g1 ~ 0 | factor(industry):factor(date), data = d1)$residuals

idx <- which(x > -1 & x < 1)

source('../binscatter.R')

par(lwd = 4)
binscatter(x[idx],y[idx])
